/**
 * 
 */
package com.iss.card.view.overrideview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.iss.card.R;
import com.iss.card.common.CardTypeUtil;
import com.iss.card.common.FunctionUtil;

/**
 * @author dtdh
 * 
 */
public class CardDetailMyCardInfoView extends View {
	private final static String TAG = CardDetailMyCardInfoView.class
			.getSimpleName();
	private int mType;
	private Paint mPaint;
	private int mAscent;
	private int mMaxWidth;
	private int mMaxHeight;

	private String mNumber;
	private String mScore;
	private String mRemainValue;
	private String mDiscount;
	private final float size_30sp = getResources().getDimension(
			R.dimen.DIM_FONT_SIZE_30SP);

	/**
	 * @param context
	 */
	public CardDetailMyCardInfoView(Context context) {
		super(context);
		init();
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public CardDetailMyCardInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public CardDetailMyCardInfoView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public void setCardTypeInformation(int type, String number, String score,
			String remainValue, String discount) {
		Log.d(TAG, "setCardTypeInformation: type=" + type);
		mType = type;
		mNumber = number;
		mScore = score;
		mRemainValue = remainValue;
		mDiscount = discount;
		requestLayout();
		invalidate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		final int num = CardTypeUtil.getCardTypeAttributeNum(mType);
		Log.d(TAG, "onDraw: num=" + num + ", type=" + mType);
		switch (num) {
		case 1:
			createOneItem(canvas);
			break;
		case 2:
			createTwoItems(canvas);
			break;
		case 3:
			createThreeItems(canvas);
			break;
		default:
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onMeasure(int, int)
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private void init() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);

		mPaint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_17SP));
		// Must manually scale the desired text size to match screen density
		// mPaint.setTextSize(17 * getResources().getDisplayMetrics().density);
		mPaint.setColor(getResources().getColor(
				R.color.color_card_detail_mycard_text));
		setPadding(3, 3, 3, 3);
	}

	private void createOneItem(Canvas canvas) {
		String title = "";
		String value = "";
		String unit = "";
		if (CardTypeUtil.isCardTypeDiscount(mType)) {
			unit = getResources().getString(R.string.discounts_unit);
			title = getResources().getString(R.string.vip_discounts);
			value = mDiscount;
		} else if (CardTypeUtil.isCardTypeMoney(mType)) {
			unit = getResources().getString(R.string.store_value_unit);
			title = getResources().getString(R.string.store_value_balance);
			value = mRemainValue;
		} else if (CardTypeUtil.isCardTypeNumber(mType)) {
			unit = getResources().getString(R.string.number_unit);
			title = getResources().getString(R.string.remain_num);
			value = mNumber;
		} else if (CardTypeUtil.isCardTypeScore(mType)) {
			unit = getResources().getString(R.string.integral_unit);
			title = getResources().getString(R.string.vip_integral);
			value = mScore;
		}

		// draw title. align left in parent.
		mPaint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_17SP));
		float interval = getResources().getDimension(
				R.dimen.DIM_RIGHT_MARGIN_FOR_CARD_DETAIL_UNIT);
		float unit_x = getPaddingLeft() + interval;
		float unit_y = (mMaxHeight - getPaddingTop() - getPaddingBottom() - mPaint
				.ascent()) / 2;
		Log.d(TAG, "createOneItem: " + unit_x + ", " + unit_y + ", " + unit
				+ ", " + title);
		canvas.drawText(title, unit_x, unit_y, mPaint);

		// draw unit. align right in parent.
		unit_x = mMaxWidth - mPaint.measureText(unit) - getPaddingRight()
				- interval;
		canvas.drawText(unit, unit_x, unit_y, mPaint);

		// draw value.
		final float textSize = mPaint.getTextSize();
		mPaint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_30SP));
		unit_x -= mPaint.measureText(value);
		canvas.drawText(value, unit_x, unit_y + 5, mPaint);
		mPaint.setTextSize(textSize);
	}

	private void createTwoItems(Canvas canvas) {
		int itemIndex = 0;
		float interval = getResources().getDimension(
				R.dimen.DIM_RIGHT_MARGIN_FOR_CARD_DETAIL_UNIT);
		float x = getPaddingLeft() + interval;
		// average the max height into 2 portions.
		float y = (mMaxHeight - getPaddingTop() - getPaddingBottom()) / 2 - 5;
		// average the max width into 2 portions.
		float offset_x = (mMaxWidth - x - getPaddingRight()) / 2;
		String title = "";
		String unit = "";
		Bitmap separator = null;
		try {
			separator = BitmapFactory.decodeResource(getResources(),
					R.drawable.card_attribute_seperator);
		} catch (OutOfMemoryError e) {
			FunctionUtil.recycleMemory();
			e.printStackTrace();
		}

		if (CardTypeUtil.isCardTypeDiscount(mType)) {
			title = getResources().getString(R.string.vip_discounts);
			unit = getResources().getString(R.string.discounts_unit);
			x = (offset_x - mPaint.measureText(title)) / 2;
			drawTextForTwoItem(canvas, mPaint, x, y, title, mDiscount, unit);
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeMoney(mType)) {
			title = getResources().getString(R.string.store_value_balance);
			unit = getResources().getString(R.string.store_value_unit);
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}
			x += (offset_x - mPaint.measureText(title)) / 2;

			drawTextForTwoItem(canvas, mPaint, x, y, title, mRemainValue, unit);
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeNumber(mType)) {
			title = getResources().getString(R.string.remain_num);
			unit = getResources().getString(R.string.number_unit);
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}
			x += (offset_x - mPaint.measureText(title)) / 2;

			drawTextForTwoItem(canvas, mPaint, x, y, title, mNumber, unit);
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeScore(mType)) {
			title = getResources().getString(R.string.vip_integral);
			unit = getResources().getString(R.string.integral_unit);
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}
			x += (offset_x - mPaint.measureText(title)) / 2;

			drawTextForTwoItem(canvas, mPaint, x, y, title, mScore, unit);
		}
	}

	private void createThreeItems(Canvas canvas) {
		int itemIndex = 0;
		float interval = getResources().getDimension(
				R.dimen.DIM_RIGHT_MARGIN_FOR_CARD_DETAIL_UNIT);
		float x = getPaddingLeft() + interval;
		// average the max height into 2 portions.
		float y = (mMaxHeight - getPaddingTop() - getPaddingBottom()) / 2 - 5;
		// average the max width into 3 portions.
		float offset_x = (mMaxWidth - x - getPaddingRight()) / 3;
		String title = "";
		String unit = "";
		Bitmap separator = null;
		try {
			separator = BitmapFactory.decodeResource(getResources(),
					R.drawable.card_attribute_seperator);
		} catch (OutOfMemoryError e) {
			FunctionUtil.recycleMemory();
			e.printStackTrace();
		}

		mPaint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_22SP));
		if (CardTypeUtil.isCardTypeDiscount(mType)) {
			
			/*title = getResources().getString(R.string.vip_discounts);
			unit = getResources().getString(R.string.discounts_unit);*/
			title = getResources().getString(R.string.vip_integral);
			unit = getResources().getString(R.string.integral_unit);
			
			x = (offset_x - mPaint.measureText(title)) / 2;
//			drawTextForThreeItem(canvas, mPaint, x, y, title, mDiscount, unit);
			drawTextForThreeItem(canvas, mPaint, x, y, title, mScore, unit);
			
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeMoney(mType)) {
			title = getResources().getString(R.string.store_value_balance);
			unit = getResources().getString(R.string.store_value_unit);
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}

			x += (offset_x - mPaint.measureText(title)) / 2;
			drawTextForThreeItem(canvas, mPaint, x, y, title, mRemainValue,
					unit);
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeNumber(mType)) {
			title = getResources().getString(R.string.remain_num);
			unit = getResources().getString(R.string.number_unit);
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}
			x += (offset_x - mPaint.measureText(title)) / 2;

			drawTextForThreeItem(canvas, mPaint, x, y, title, mNumber, unit);
			itemIndex++;
		}

		if (CardTypeUtil.isCardTypeScore(mType)) {
			/*title = getResources().getString(R.string.vip_integral);
			unit = getResources().getString(R.string.integral_unit);*/
			
			title = getResources().getString(R.string.vip_discounts);
			unit = getResources().getString(R.string.discounts_unit);
			
			x = itemIndex * offset_x + 5;
			if (itemIndex > 0) {
				canvas.drawBitmap(separator, x, interval, mPaint);
			}
			x += (offset_x - mPaint.measureText(title)) / 2;

//			drawTextForThreeItem(canvas, mPaint, x, y, title, mScore, unit);
			drawTextForThreeItem(canvas, mPaint, x, y, title, mDiscount, unit);
		}
	}

	private void drawTextForThreeItem(Canvas canvas, Paint paint, float x,
			float y, String title, String value, String unit) {
		final float offset_y = getResources().getDimension(
				R.dimen.DIM_RIGHT_MARGIN_FOR_CARD_DETAIL_UNIT) * 2;

		// draw title.
		paint.setFakeBoldText(false);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_16SP));
		canvas.drawText(title, x, y, paint);

		// draw value.
		paint.setFakeBoldText(true);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_18SP));
		canvas.drawText(value, x, y + paint.descent() + offset_y, paint);

		// draw unit.
		x += paint.measureText(value);
		paint.setFakeBoldText(false);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_11SP));
		canvas.drawText(unit, x, y + paint.descent() + offset_y, paint);
	}

	private void drawTextForTwoItem(Canvas canvas, Paint paint, float x,
			float y, String title, String value, String unit) {
		final float offset_y = getResources().getDimension(
				R.dimen.DIM_RIGHT_MARGIN_FOR_CARD_DETAIL_UNIT) * 2;
		// draw title.
		paint.setFakeBoldText(false);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_17SP));
		canvas.drawText(title, x, y, paint);

		// draw value.
		paint.setFakeBoldText(true);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_18SP));
		canvas.drawText(value, x, y + paint.descent() + offset_y, paint);

		// draw unit.
		x += paint.measureText(value);
		paint.setFakeBoldText(false);
		paint.setTextSize(getResources().getDimension(
				R.dimen.DIM_FONT_SIZE_11SP));
		canvas.drawText(unit, x, y + paint.descent() + offset_y, paint);
	}

	/**
	 * Determines the width of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		Log.d(TAG, "measureWidth: mw=" + measureSpec + ", s=" + specSize);
		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			result = specSize;
		} else {
			// Measure the text
			// result = (int) mPaint.measureText(mText) + getPaddingLeft()
			// + getPaddingRight();
			result = specSize;
			if (specMode == MeasureSpec.AT_MOST) {
				// Respect AT_MOST value if that was what is called for by
				// measureSpec
				result = Math.min(result, specSize);
			}
		}

		mMaxWidth = result;
		return result;
	}

	/**
	 * Determines the height of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		Log.d(TAG, "measureHeight: mw=" + measureSpec + ", s=" + specSize);
		mPaint.setTextSize(size_30sp);
		mAscent = (int) mPaint.ascent();
		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			result = specSize;
		} else {
			// // Measure the text (beware: ascent is a negative number)
			result = (int) (-mAscent + mPaint.descent()) * 2 + getPaddingTop()
					+ getPaddingBottom();
			// result = specSize;
			if (specMode == MeasureSpec.AT_MOST) {
				// Respect AT_MOST value if that was what is called for by
				// measureSpec
				result = Math.min(result, specSize);
			}
		}
		Log.d(TAG, "measureHeight: result=" + result + ", asc=" + mAscent
				+ ", des=" + mPaint.descent());
		mMaxHeight = result;
		return result;
	}
}
